//
//  MyXYCardController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/8.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "MyXYCardController.h"
#import "MyXYCardHeadView.h"
#import "MyXYCardViewCell.h"
#import "MyXYCardModel.h"

#import "AddXYCardController.h"
#import "SetPlanController.h"
#import "PlanNewsController.h"

#import "AddXYCardController.h"

@interface MyXYCardController ()<UITableViewDelegate,UITableViewDataSource,MyXYCardViewCellDelegate,MyXYCardHeadViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *CardTable;
@property (strong, nonatomic) NSMutableArray * cardArr;
@property (strong, nonatomic) MBProgressHUD * hud;
@property (strong, nonatomic) UIButton * addBtn;


@end

@implementation MyXYCardController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.cardArr removeAllObjects];
    [self UpLoadDataOfMyXYCardController];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"智能还款";
    
    self.addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.addBtn setImage:[UIImage imageNamed:@"ZNHK_AddImage01"] forState:UIControlStateNormal];
    [self.addBtn addTarget:self action:@selector(ClickOnBtnOfMyXYCardViewCellToAddNewCard) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * addItem = [[UIBarButtonItem alloc] initWithCustomView:self.addBtn];
    self.navigationItem.rightBarButtonItem = addItem;
    
    self.CardTable.delegate = self;
    self.CardTable.dataSource = self;
    self.CardTable.rowHeight = 140;
    self.CardTable.backgroundColor = [UIColor clearColor];
    self.CardTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.CardTable registerNib:[UINib nibWithNibName:@"MyXYCardViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    
}

- (void)UpLoadDataOfMyXYCardController{
    
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    self.hud = [MainObjectWay CreateHUDView: self.view];
    
    [MainObjectWay PostWithMethod:@"list" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                MyXYCardModel * model = [MyXYCardModel mj_objectWithKeyValues:dic];
                [self.cardArr addObject:model];
            }
        }
        [self.CardTable reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hideAnimated:YES];
        NSLog(@"错误信息 = %@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cardArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyXYCardViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MyXYCardViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.tag = indexPath.row;
    cell.delegate = self;
    [cell CreateMyXYCardViewCell];
    MyXYCardModel * model = self.cardArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        MyXYCardModel * model = self.cardArr[indexPath.row];
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
        [dic setObject:model.card_id forKey:@"card_id"];
        [dic setObject:KUserID forKey:@"card_member"];
        
        [MainObjectWay PostWithMethod:@"remove" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
            NSLog(@"返回的数据 = %@",responseObject);
            if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                [self.cardArr removeAllObjects];
                [self UpLoadDataOfMyXYCardController];
            }else{
                [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
            }
        } failure:^(NSError *error) {
            NSLog(@"错误信息 = %@",error);
        }];
    }];
    deleteAction.backgroundColor = [UIColor orangeColor];
    
    return @[deleteAction];
}




- (void)ClickOnBtnOfMyXYCardViewCellToBottonBtn01:(NSUInteger)cellID{
    MyXYCardModel * model = self.cardArr[cellID];
    if ([model.card_status isEqual:@"3"]) {
        PlanNewsController * PlanC = [[PlanNewsController alloc] init];
        PlanC.ShowBot = NO;
        PlanC.CardModel = model;
        PlanC.DataDic = nil;
        [self.navigationController pushViewController:PlanC animated:YES];
        
        
    }else{
        [MBProgressHUD showAlertViewWithText:@"计划待执行"];
    }
    
}

- (void)ClickOnBtnOfMyXYCardViewCellToBottonBtn02:(NSUInteger)cellID{
    MyXYCardModel * model = self.cardArr[cellID];
    
    if ([model.card_bind intValue] == 0) {
        AddXYCardController * vc = [[AddXYCardController alloc]init];
        vc.card_id = model.card_id;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        if ([model.card_status isEqual:@"3"]) {
            PlanNewsController * PlanC = [[PlanNewsController alloc] init];
            PlanC.ShowBot = NO;
            PlanC.CardModel = model;
            PlanC.DataDic = nil;
            [self.navigationController pushViewController:PlanC animated:YES];
            
        }else{
            MyXYCardModel * model = self.cardArr[cellID];
            SetPlanController * PlanC = [[SetPlanController alloc] init];
            PlanC.headModel = model;
            [self.navigationController pushViewController:PlanC animated:YES];
        }
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.cardArr.count == 0) {
        return KScreenH-TopH-BotH;
    }else{
        return 50;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    MyXYCardHeadView * headV = [[NSBundle mainBundle] loadNibNamed:@"MyXYCardHeadView" owner:self options:nil][0];
    headV.delegate = self;
    if (self.cardArr.count == 0) {
        self.addBtn.hidden = YES;
        headV.HeadBackView01.hidden = NO;
        headV.HeadBackView02.hidden = YES;
    }else{
        self.addBtn.hidden = NO;
        headV.HeadBackView01.hidden = YES;
        headV.HeadBackView02.hidden = NO;
    }
    
    return headV;
}

- (void)ClickOnBtnToPushAddCard {
    [self ClickOnBtnOfMyXYCardViewCellToAddNewCard];
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 4;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 4)];
    footV.backgroundColor = [UIColor clearColor];
    return footV;
}




- (void)ClickOnBtnOfMyXYCardViewCellToAddNewCard{
    AddXYCardController * AddC = [[AddXYCardController alloc] init];
    [self.navigationController pushViewController:AddC animated:YES];
}

- (NSMutableArray *)cardArr {
    if (!_cardArr) {
        _cardArr = [NSMutableArray array];
    }
    return _cardArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
